<?php

namespace App\Services;

use Illuminate\Support\Facades\Log;

class RpcService
{
    // RPC请求
    public static function rpcRequest($url, $method, $data)
    {
        $baseUrl = config('app.rpcserver');
        $resp = request($baseUrl . $url, $method, $data);
        $ret = json_decode($resp->getBody()->getContents(), true);
        if (!isset($ret['code'])) {
            $ret['code'] = -1;
        }
        // 记录失败LOG
        if ($resp->getStatusCode() != 200 || !isset($ret['code']) || $ret['code'] != 0) {
            Log::error("RPC Request Error: ", ['url' => $url, 'method' => $method, 'data' => $data, 'status' => $resp->getStatusCode(), 'ret' => $ret]);
        }
        return $ret;
    }
}